package com.power.using.mydaggerp.presenter.movie;

import android.support.annotation.NonNull;
import android.widget.ImageView;

import com.power.using.mydaggerp.contract.movie.TopMovieContract;
import com.power.using.mydaggerp.model.bean.douban.movie.HotMovieBean;
import com.power.using.mydaggerp.model.bean.douban.movie.child.SubjectsBean;
import com.power.using.mydaggerp.model.movie.TopMovieModel;
import com.power.using.mydaggerp.ui.activity.detial.MovieDetailActivity;

import io.reactivex.functions.Consumer;

/**
 * Created by 聪明一只哈 on 2019/10/17.
 * Time: 2019/10/17  10:46
 */
public class TopMoviePresenter extends TopMovieContract.TopMoviePresenter{

    private int mStart;
    private int mCount = 30;
    private boolean isLoading;


    @NonNull
    public static TopMoviePresenter newInstance() {
        return new TopMoviePresenter();
    }


    @Override
    public void loadTopMovieList() {

        if(mIModel==null||mIView==null){
            return;
        }
        mStart=0;
        //一次加载20条
        mRxManage.register(mIModel.getTopMOvieList(mStart,mCount).subscribe(new Consumer<HotMovieBean>() {
            @Override
            public void accept(HotMovieBean hotMovieBean) throws Exception {

                if(mIView==null){
                    return;
                }

                mStart+=mCount;
                mIView.updateContentList(hotMovieBean.getSubjects());
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                if(mIView==null){
                    return;
                }

                if(mIView.isVisiable()){
                    mIView.showToast("Network error");
                }

                mIView.showNetworkError();
            }
        }));



    }

    @Override
    public void loadMoreTopMovie() {
        if(!isLoading){
            isLoading=true;
           //一次20条
            mRxManage.register(mIModel.getTopMOvieList(mStart,mCount).subscribe(new Consumer<HotMovieBean>() {
                @Override
                public void accept(HotMovieBean hotMovieBean) throws Exception {
                    isLoading = false;
                    if(mIView==null){
                        return;
                    }

                    if(hotMovieBean!=null&&
                            hotMovieBean.getSubjects()!=null&&
                            hotMovieBean.getSubjects().size()>0){
                        mStart+=mCount;
                        mIView.updateContentList(hotMovieBean.getSubjects());
                    }else{
                        mIView.showNoMoreData();
                    }
                }
            }, new Consumer<Throwable>() {
                @Override
                public void accept(Throwable throwable) throws Exception {
                    isLoading = false;
                    if (mIView != null) {
                        mIView.showLoadMoreError();
                    }
                }
            }));




        }

    }

    @Override
    public void onItemClick(int position, SubjectsBean item, ImageView imageView) {
        MovieDetailActivity.start(mIView.getBindActivity(), item, imageView);

    }

    @Override
    protected TopMovieContract.ITopMovieModel getModel() {
        return TopMovieModel.newInstance();
    }

    @Override
    public void onStart() {

    }
}
